﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<App.Views.ViewModels.FarewellViewModel>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="keywords" content="jquery,ui,easy,easyui,web">
	<meta name="description" content="easyui help you build your web page easily!">
	<title>服务收费项目管理</title>
    <link rel="stylesheet" type="text/css" href="<%: Url.Content("~/Res/jquery-easyui-1.2.4/themes/default/easyui.css") %>"  />
    <link rel="stylesheet" type="text/css" href="<%: Url.Content("~/Res/jquery-easyui-1.2.4/themes/icon.css") %>"  />	  
	<style type="text/css">
		#fm{
			margin:0;
			padding:10px 30px;
		}
		.ftitle{
			font-size:14px;
			font-weight:bold;
			color:#666;
			padding:5px 0;
			margin-bottom:10px;
			border-bottom:1px solid #ccc;
		}		
	</style>
    <script type="text/javascript" src="<%: Url.Content("~/Res/jquery-easyui-1.2.4/jquery-1.6.min.js") %>" ></script>
    <script type="text/javascript" src="<%: Url.Content("~/Res/jquery-easyui-1.2.4/jquery.easyui.min.js") %>" ></script>    
	<script type="text/javascript" src="<%: Url.Content("~/Res/jquery-easyui-1.2.4/locale/easyui-lang-zh_CN.js") %>" ></script>
    
    <script type="text/javascript">
        function Crud(id, controllerName) {
            var url;
            this.Details = function () { };
            this.Select = function () { };
            this.Create = function () {
                url = '../' + controllerName + '/Create';
                var row = $('#dg').treegrid('getSelected');
                if (row) {
                    var p = $('#dg').treegrid('getParent', row[id]);
                    if (p) {
                        $.messager.alert("提示", "请选择一大类进行添加!");
                        return;
                    }
                }
                $('#' + id).removeAttr('readonly');
                $('#dlg').dialog('open').dialog('setTitle', '新增');
                $('#fm').form('clear');
                if (row) {

                    $('#fm').form('load', { ParentServiceID: row['ItemNO'] });
                }
                currId = "";
            };
            this.Edit = function () {
                var row = $('#dg').treegrid('getSelected');
                if (row) {
                    $('#dlg').dialog('open').dialog('setTitle', '修改');
                    $('#fm').form('load', row);
                    if (row['Required']) $('#Required').attr('checked', 'checked');
                    $('#'+id).attr('readonly', 'readonly');
                    url = '../' + controllerName + '/Edit?id=' + row[id];
                    currId = row[id];
                } else {
                    $.messager.alert("提示", "请选择一条记录进行编辑!");
                }
            };
            this.Save = function () {
                var form = $('#fm');
                if (!$(form).form('validate')) {
                    return;
                }
                $.post(url, $(form).serialize(), function (result) {
                    if (result.success) {
                        $('#dlg').dialog('close');          // close the dialog
                        $('#dg').treegrid('reload');        // reload the user data
                    } else {
                        $.messager.show({
                            title: '错误',
                            msg: result.msg
                        });
                    }
                });
            };
            this.Cancel = function () {
                $('#dlg').dialog('close');
            };
            this.Delete = function () {
                var ids = [];
                var rows = $('#dg').treegrid('getSelections');
                for (var i = 0; i < rows.length; i++) {
                    ids.push(rows[i][id]);
                }
                if (ids.length < 1) {
                    $.messager.alert("提示", "请至少选择一条记录进行删除!");
                    return;
                }
                $.messager.confirm('Confirm', '你确定要删除吗?', function (r) {
                    if (r) {
                        $.post('../' + controllerName + '/Delete', { ids: ids.join("&") }, function (result) {
                            if (result.success) {
                                $('#dg').treegrid('reload');
                            } else {
                                $.messager.show({	// show error message
                                    title: '错误',
                                    msg: result.msg
                                });
                            }
                        }, 'json');
                    }
                });
            };
        }
        //工具栏
        var toolbar = [
                        {
                            id: 'btnCreate',
                            text: '创建',
                            iconCls: 'icon-add',
                            handler: function () {
                                crud.Create();
                            }
                        }, '-', {
                            id: 'btnDelete',
                            text: '删除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                crud.Delete();
                            }
                        }, '-', {
                            id: 'btnEdit',
                            text: '修改',
                            iconCls: 'icon-edit',
                            handler: function () {
                                crud.Edit();
                            }
                        }];	          

        var id = 'ItemNO';
        var ctrlName = 'ServicesItem';
        var crud = new Crud(id, ctrlName);
        $(function () {
            $('#dg').treegrid({
                title: '服务项目列表',
                iconCls: 'icon-save',
                nowrap: false,
                animate: true,
                collapsible: true,
                url: '../' + ctrlName + '/List',
                idField: id,
                treeField: id,
                frozenColumns: [[
	                { title: '编号', field: 'ItemNO', width: 150,
	                    formatter: function (value) {
	                        return '<span style="color:red">' + value + '</span>';
	                    }
	                }
				]],
                columns: [[
					{ field: 'ItemName', title: '名称', width: 150 },
					{ field: 'Unit', title: '单位', width: 80 },
					{ field: 'Price', title: '单价', width: 80 },
                    { field: 'Required', title: '必选', width: 80,
                        formatter: function (value) {
                            var str = '<input type="checkbox"  disabled="disabled" ';
                            if (value) str = str + 'checked="checked" ';
                            str = str + "/>";
                            return str;
                        }
                    },
					{ field: 'ServiceContent', title: '描述', width: 160 }
				]],
//                onBeforeLoad: function (row, param) {
//                    var u = '../' + ctrlName + '/List';
//                    if (row) {
//                        $(this).treegrid('options').url = u + "?id=" + row[id];
//                    } else {
//                        $(this).treegrid('options').url = u + "?id=";
//                    }
//                },
                toolbar: toolbar
            });
        });
    </script>
</head>
<body>
	<table id="dg" ></table>
    
    <div id="dlg" class="easyui-dialog" closed="true" buttons="#dlg-buttons" 
			style="width:550px;height:280px;padding:10px 20px">
		<div class="ftitle">服务项目</div>
		<form id="fm" method="post"> 
            <table width="99%">
                <tr>
                    <td width="15%">编号</td>
                    <td width="35%"><input id="ItemNO" name="ItemNO" class="easyui-validatebox" validType="length[4,4]" required="true" /></td>
                    <td  width="15%">名称</td>
                    <td width="35%"><input name="ItemName" class="easyui-validatebox" required="true" /></td>
                </tr>
                <tr>
                    <td>单位</td>
                    <td><input name="Unit" class="easyui-validatebox" /></td>
                    <td>单价</td>
                    <td><input name="Price" id="Price" class="easyui-validatebox" />
                        <script type="text/javascript">
                            $('#Price').numberbox({
                                min: 0,
                                precision: 2
                            });
                        </script>
                    </td>
                </tr>
                <tr>
                    <td>必选</td>
                    <td><input type="checkbox" name="Required" id="Required" /></td>
                    <td>父类</td>
                    <td><input name="ParentServiceID" class="easyui-validatebox" /></td>
                </tr>
                <tr>
                    <td>描述</td>
                    <td colspan="3"><textarea name="ServiceContent" class="easyui-validatebox" style="width:98%"></textarea></td>                    
                </tr>
            </table>           						
		</form>
	</div>
	<div id="dlg-buttons">
		<a href="#" id="btnSave" class="easyui-linkbutton" iconCls="icon-ok" onclick="crud.Save()" >保存</a>
		<a href="#" id="btnCancel" class="easyui-linkbutton" iconCls="icon-cancel" onclick="crud.Cancel()" >取消</a>
	</div>
</body>
</html>
